package com.xgpro.common.websocket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.annotation.Resource;

/**
 * @ClassName WebSocketConfig
 * @Author: likai
 * @Date: 2019-09-07 11:23
 * @Version 1.0
 * @Description websocket配置类
 */

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    private static final Logger logger = LoggerFactory.getLogger(WebSocketConfig.class);

    @Resource
    private CustomHandler handler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //注册webSocket server实现类
        registry.addHandler(handler,"/webSocketServer").addInterceptors(new WebSocketInterceptor()).setAllowedOrigins("*");
        //使用sockjs的注册方法
        registry.addHandler(handler,"/sockjs/webSocketServer").addInterceptors(new WebSocketInterceptor()).withSockJS();
    }
}
